#include<bits/stdc++.h>
using namespace std;

#define int long long
#define ls (p<<1)
#define rs (p<<1|1)
#define mid ((l+r)>>1)
#define ms0(a) memset(a,0,sizeof a)
#define msp(a) memset(a,0x3f,sizeof a)
#define msn(a) memset(a,-0x3f,sizeof a)
#define mc(a,b) memcpy(a,b,sizeof b)
#define endl '\n'
#define pii pair<int,int>

constexpr int mod=1e9+7;
constexpr int N=2e5+7,M=1e6+7;
constexpr int mx=1000000;
bool bg_m;
int Case=1;
inline void cmax(int &a,int b){a=a<b?b:a;}
inline void cmin(int &a,int b){a=a>b?b:a;}
inline int qp(int a,int b){int res=1;
while(b){if(b&1)res=1ll*res*a%mod;a=1ll*a*a%mod;b>>=1;}return res;}

int n;
int a[N];
int dp[N],ans;
int t[M<<2],tg[M<<2];
void pushup(int p){t[p]=max(t[ls],t[rs]);}
void pushdown(int p){
    if(!tg[p]) return;
    t[ls]+=tg[p];
    t[rs]+=tg[p];
    tg[ls]+=tg[p];
    tg[rs]+=tg[p];
    tg[p]=0;
}
void build(int p,int l,int r){
    tg[p]=0;
    t[p]=-0x3f3f3f3f3f3f3f3f;
    if(l==r) return;
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(p);
}
void upd(int p,int l,int r,int x,int y){
    if(l==r) return cmax(t[p],y);
    pushdown(p);
    if(x<=mid) upd(ls,l,mid,x,y);
    else upd(rs,mid+1,r,x,y);
    pushup(p);
}
int ask(int p,int l,int r,int ll,int rr){
    if(ll>rr) return -0x3f3f3f3f3f3f3f3f;
    if(ll<=l&&rr>=r) return t[p];
    pushdown(p);
    int ans=-0x3f3f3f3f3f3f3f3f;
    if(ll<=mid) cmax(ans,ask(ls,l,mid,ll,rr));
    if(rr>mid) cmax(ans,ask(rs,mid+1,r,ll,rr));
    return ans;
}
int ask(int p,int l,int r,int x){
    if(l==r) return t[p];
    pushdown(p);
    if(x<=mid) return ask(ls,l,mid,x);
    return ask(rs,mid+1,r,x);
}

void Main(){

    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    build(1,1,mx);
    // cout<<dp[1]<<endl;
    for(int i=2;i<=n;i++){
        // for(int j=1;j<i-1;j++){
        //     dp[i][j]=dp[i-1][j]+a[i]*(a[i]==a[i-1]);
        // }
        if(i>2) upd(1,1,mx,a[i-2],dp[i-1]);
        // cmax(mxval[a[i-2]],dp[i-1]);
        dp[i]=0;
        cmax(dp[i],ask(1,1,mx,1,a[i]-1));
        cmax(dp[i],ask(1,1,mx,a[i]+1,mx));
        cmax(dp[i],ask(1,1,mx,a[i])+a[i]);
        if(a[i]==a[i-1]) t[1]+=a[i],tg[1]+=a[i];
        // for(int j=1;j<=2000;j++){
        //     cmax(dp[i],mxval[j]+a[i]*(a[i]==j));
        // }
        // for(int j=1;j<=2000;j++) mxval[j]+=a[i]*(a[i]==a[i-1]);
        // cout<<dp[i]<<endl;
    }
    ans=dp[n];
    cmax(ans,t[1]);
    cout<<ans<<endl;

    return;
}
string RdFile="color";
bool en_m;
signed main(){
    freopen((RdFile+".in").c_str(),"r",stdin);
    freopen((RdFile+".out").c_str(),"w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>Case;
    while(Case--) Main();
    // cerr<<(&en_m-&bg_m)/1024.0/1024<<endl;
    return 0;
}
/*
g++ color.cpp -o color -g -Wall -Wextra -Wshadow -fsanitize=address,undefined -std=c++14
g++ color.cpp -o color ‐O2 ‐std=c++14 ‐static
*/